package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.GroupTalkBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.definterface.PicChooseWithOutCutCallBack;
import com.xmb.wechat.lsn.SendMsgCenter;
import com.xmb.wechat.util.PicChooseUtils;
import com.xmb.wechat.util.PicLoadUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatChatAddImgActivity extends BaseActivity {
    public static final int IMAGE_PICKER = 100;

    @BindView(R2.id.tv_sender_name)
    TextView       tvSenderName;
    @BindView(R2.id.iv_avatar)
    ImageView      ivAvatar;
    @BindView(R2.id.iv_arrow)
    ImageView      ivArrow;
    @BindView(R2.id.btn_pick_sender)
    RelativeLayout btnPickSender;
    @BindView(R2.id.rv)
    RecyclerView   rv;
    @BindView(R2.id.iv_img)
    ImageView      ivImg;
    @BindView(R2.id.iv_arrow2)
    ImageView      ivArrow2;
    @BindView(R2.id.btn_pick_img)
    RelativeLayout btnPickImg;
    @BindView(R2.id.btn_save)
    Button         btnSave;

    private WechatContactBean       sender;
    private WechatContactBean       mWechatContactBean;
    private GroupTalkBean           mGroupTalkBean;
    private boolean                 isRoomChat;
    private ContactAdapter          adapter;
    private List<WechatContactBean> list;

    public WechatChatAddImgActivity() {
        super(R.layout.wechat_activity_chat_send_img);
    }

    public static void start(Activity act, WechatContactBean contactBean) {
        Intent it = new Intent(act, WechatChatAddImgActivity.class);
        it.putExtra("isRoomChat", false);
        it.putExtra("WechatContactBean", contactBean);
        act.startActivity(it);
    }

    public static void start(Context act, GroupTalkBean groupTalkBean) {
        Intent it = new Intent(act, WechatChatAddImgActivity.class);
        it.putExtra("isRoomChat", true);
        it.putExtra("GroupTalkBean", groupTalkBean);
        act.startActivity(it);
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        //添加数据（me）
        WechatContactBean me = WechatContactBean.instanceNewContactMe(this);
        onSenderChange(me);
        list.add(me);
        //添加数据（other）
        isRoomChat = getIntent().getBooleanExtra("isRoomChat", false);
        if (!isRoomChat) {
            mWechatContactBean = (WechatContactBean) getIntent().getSerializableExtra("WechatContactBean");
            list.add(mWechatContactBean);
        } else {//群聊
            mGroupTalkBean = (GroupTalkBean) getIntent().getSerializableExtra("GroupTalkBean");
            list.addAll(mGroupTalkBean.getGroupMembersList());
        }
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        adapter = new ContactAdapter(R.layout.wechat_select_sender_contact_item, list);
        rv.setAdapter(adapter);
    }

    @OnClick({R2.id.btn_pick_sender, R2.id.btn_pick_img, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_pick_sender) {
            if (rv.getVisibility() == View.VISIBLE) {
                rv.setVisibility(View.GONE);
            } else {
                rv.setVisibility(View.VISIBLE);
            }
        } else if (i == R.id.btn_pick_img) {
            PicChooseUtils.chooseOnePicWithOutCut(this, new PicChooseWithOutCutCallBack() {
                @Override
                public void onPicChoose(String... Pic) {
                    if (TextUtils.isEmpty(Pic[0]))
                        return;
                    pickImgPath = Pic[0];
                    PicLoadUtils.loadSdcardPic(Pic[0], ivImg);
                }
            });
        } else if (i == R.id.btn_save) {
            onClickSend();
        }
    }

    private String pickImgPath;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_PICKER) {//返回多张照片
            String pic = getPicFromIntent(data);
            if (TextUtils.isEmpty(pic))
                return;
            pickImgPath = pic;
            PicLoadUtils.loadSdcardPic(pic, ivImg);
        }
    }

    private void onClickSend() {
        SendMsgCenter.sendImg(new SendMsgCenter.SendMsgListener() {
            @Override
            public void onSendSuc() {
                finish();
            }

            @Override
            public void onSendError(String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        }, sender.getId(), isRoomChat, isRoomChat ? mGroupTalkBean.getId() : mWechatContactBean.getId(), pickImgPath);
    }

    public class ContactAdapter extends BaseQuickAdapter<WechatContactBean, BaseViewHolder> {
        public ContactAdapter(int layoutResId, List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final WechatContactBean vo) {
            helper.setText(R.id.name, vo.getName());
            WechatContactBean.setupAvatarIntoImageView(vo,
                    (ImageView) helper.getView(R.id.iv_avatar), 12);
            helper.setChecked(R.id.rb, vo.isSelected());
            helper.getView(R.id.vg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (WechatContactBean item : ContactAdapter.this.getData()) {
                        item.setSelected(false);
                    }
                    notifyDataSetChanged();
                    onSenderChange(vo);
                }
            });
        }
    }

    private void onSenderChange(WechatContactBean vo) {
        vo.setSelected(true);
        sender = vo;
        rv.setVisibility(View.GONE);
        WechatContactBean.setupAvatarIntoImageView(vo, ivAvatar, 12);
        tvSenderName.setText(vo.getName());
    }
}
